package com.cmcc.linlaker.ui.query;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.android.volley.VolleyError;
import com.cmcc.linlaker.R;
import com.cmcc.linlaker.fragment.RoundFragment;
import com.cmcc.linlaker.http.ApiDefine;
import com.cmcc.linlaker.ui.mine.MessageActivity;
import com.cmcc.linlaker.ui.query.adapter.QueryPageAdapter;
import com.cmcc.linlaker.ui.query.bean.ProjectDetailBean;
import com.cmcc.linlaker.ui.query.bean.QueryProjectBean;
import com.cmcc.linlaker.ui.query.fragment.FarmerApproveDetailFragment;
import com.cmcc.linlaker.ui.query.fragment.FarmerBaseDetailFragment;
import com.cmcc.linlaker.utils.BarUtils;
import com.cmcc.linlaker.utils.IntentUtils;
import com.cmcc.linlaker.utils.SystemUtils;
import com.cmcc.linlaker.utils.ToastUtils;
import com.cmcc.linlaker.volley.GsonRequestHelper;

import java.util.ArrayList;
import java.util.List;

public class FarmerDetailActivity extends AppCompatActivity {
    public QueryProjectBean.QueryProjectData houseData;
    private int flag = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_query_farmer_detail);

        RoundFragment.isCanClick = true;

        BarUtils.initStatusBar(this);

        houseData = (QueryProjectBean.QueryProjectData) getIntent().getSerializableExtra("houseData");
        flag = getIntent().getIntExtra("flag", 0);

        initView();
    }

    private TabLayout indicator;
    private ViewPager mViewPager;
    private QueryPageAdapter pagerAdapter;

    private void initView() {
        if (flag == 1) {
            findViewById(R.id.text_navi).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.text_navi).setVisibility(View.GONE);
        }

        initPagerAdapter();

        indicator = findViewById(R.id.indicator);
        mViewPager = findViewById(R.id.view_pager);
        mViewPager.setOffscreenPageLimit(2);

        if (houseData != null) {
            getHouseDetail();
        }
    }

    private String[] titles = {"基础信息", "审批信息"};
    private List<Fragment> fragmentList;

    /**
     * 初始化pagerAdpter
     */
    private void initPagerAdapter() {
        fragmentList = new ArrayList<>();
        fragmentList.add(FarmerBaseDetailFragment.newInstance());
        fragmentList.add(FarmerApproveDetailFragment.newInstance());

        pagerAdapter = new QueryPageAdapter(getSupportFragmentManager(), titles, fragmentList);
    }

    public void back(View view) {
        finish();
    }

    /**
     * 查询详情
     */
    private void getHouseDetail() {
        GsonRequestHelper helper = new GsonRequestHelper(this);
        String getUrl = ApiDefine.PROJECT_DETAIL + houseData.id;
        helper.sendGETRequest(getUrl, ProjectDetailBean.class, true, null);
        helper.setOnResponseListener(new GsonRequestHelper.OnResponseListener() {
            @Override
            public void onError(VolleyError error) {

            }

            @Override
            public void onSuccess(Object data) {
                if (data != null) {
                    ProjectDetailBean bean = (ProjectDetailBean) data;
                    if (bean.code.equals("0")) {
                        houseDetailData = bean.data;
                    } else if (bean.code.equals(IntentUtils.VAILD_CODE)) {
                        IntentUtils.skipToLogin(FarmerDetailActivity.this);
                    } else {
                        ToastUtils.showText(FarmerDetailActivity.this, bean.msg);
                    }

                    mViewPager.setAdapter(pagerAdapter);
                    indicator.setTabMode(TabLayout.MODE_FIXED);
                    indicator.setupWithViewPager(mViewPager);
                }
            }
        });
    }

    public QueryProjectBean.QueryProjectData houseDetailData;

    /**
     * 导航
     */
    public void skipToNavi(View view) {
        if (SystemUtils.getInstance().appIsInstalled(this, "com.autonavi.minimap")) {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_VIEW);
            intent.addCategory(Intent.CATEGORY_DEFAULT);
            String url = "androidamap://route?sourceApplication=amap&dlat=" + houseDetailData.latitude + "&dlon=" + houseDetailData.longitude + "&dname=" + houseDetailData.name + "&dev=0&t=1";
//            String url = "androidamap://route?sourceApplication=amap&dlat=" + 29.608474 + "&dlon=" + 119.008436 + "&dname=" + "项目名称" + "&dev=0&t=1";

            Uri uri = Uri.parse(url);
            //将功能Scheme以URI的方式传入data
            intent.setData(uri);
            //启动该页面即可
            startActivity(intent);
        } else {
            ToastUtils.showText(this, "暂未安装高德地图");
        }

//        Uri uri = Uri.parse("http://api.map.baidu.com/geocoder?location=" + houseDetailData.latitude + "," + houseDetailData.longitude + "&coord_type=gcj02&output=html&src=com.cmcc.linlaker");
//        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//        intent.addCategory(Intent.CATEGORY_DEFAULT);
//        startActivity(intent);

//        Intent intent = new Intent(FarmerDetailActivity.this, NaviActivity.class);
//        intent.putExtra("houseDetailData", houseDetailData);
//        startActivity(intent);
    }

}
